<template>
  <div>
    <el-dialog title="分配角色" :visible="showRoleDialog" @close="closeFn">
      <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
      <!-- 选项 -->
      <el-checkbox-group v-model="checkList">
        <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.title }}</el-checkbox>
      </el-checkbox-group>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="assignRolesFn">确定</el-button>
          <el-button size="small" @click="closeFn">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import {
  getEmployeesRole,
  getEmployeesRoleList,
  editUser
} from '@/api/employees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      checkList: [],
      roleList: [],
      editUserId: ''
    }
  },
  created() {
    this.getEmployeesRoleList()
  },
  methods: {
    // 分配角色确认按钮
    async assignRolesFn() {
      // console.log(88);
      await editUser({ id: this.editUserId, roles: this.checkList })
      this.$message.success('设置角色成功!')
      this.closeFn()
    },
    // 根据id获取用户角色
    async getEmployeesRole(id) {
      this.editUserId = id
      const res = await getEmployeesRole(id)
      console.log(42, res.roles);
      [...res.roles].forEach((item) => {
        // console.log(45, item.id);
        return this.checkList.push(item.id)
      })
      // console.log(47, this.roleList);
    },
    // 角色列表
    async getEmployeesRoleList() {
      const res = await getEmployeesRoleList({ _limit: 100, _start: 0 })
      // console.log(48, res);
      this.roleList = res
    },
    // 取消按钮
    closeFn() {
      this.$emit('update:showRoleDialog', false)
    }
  }
}
</script>

<style>
</style>
